package com.asdf.application.commands.dialogHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;

import com.asdf.application.Constants;
import com.asdf.db.DataBase;
import com.asdf.db.DataBase.DataSource;
import com.asdf.db.operations.companyclub.GetCompanyClubOp;
import com.asdf.db.operations.user.GetUserByUsernameOp;
import com.asdf.db.results.ResultStatus;
import com.asdf.db.results.companyclub.CompanyClubResult;
import com.asdf.db.results.user.UserResult;
import com.asdf.entities.CompanyClub;
import com.asdf.external.DBUserDetails;
import com.asdf.external.INetserviceSimulator;
import com.asdf.external.IVSDT;
import com.asdf.external.LogWriter;
import com.asdf.external.stubs.VSDT;
import com.asdf.ui.ConnectDialog;
import com.asdf.ui.CreditCardDetailsDialog;

/**
 * @author Artem Barger
 *
 */
public class DialogHandlerConnect implements IDialogHandler
{
	
	private static final String	CANCEL_CMD	= "CANCEL";
	private static final String	OK_CMD		= "OK";
	
	private ConnectDialog dlg;
	
	/**
	 * @param dlg reference to dialog window to which this handler is attached.
	 */
	public DialogHandlerConnect( ConnectDialog dlg)
	{
		this.dlg = dlg;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void handle( final INetserviceSimulator simulator)
	{
		
		
		final String userLogin = dlg.getUserLogin( );
		final String userPwd = dlg.getUserPwd( );
		
		dlg.dispose( );
		
		GetUserByUsernameOp op = new GetUserByUsernameOp( ( List<DBUserDetails> ) DataBase.getInstance( ).getDataSource( DataSource.UserList));
		
		op.setUsername( userLogin);
		UserResult usrResult = ( UserResult ) DataBase.getInstance( ).Do( op);
		
		if ( usrResult.getStatus( ) != ResultStatus.OK)
		{
			LogWriter.getInstance( ).reportWarning( Constants.UNABLE_CONNECT_MSG);
			return;
		}
		
		DBUserDetails user = usrResult.getUser( );
		
		GetCompanyClubOp locateClub = new GetCompanyClubOp( ( List<CompanyClub> ) DataBase.getInstance( ).getDataSource( DataSource.CompanyClubs));
		locateClub.setCompanyName( user.getAviationCompanyName( ));
		CompanyClubResult result = ( CompanyClubResult ) DataBase.getInstance( ).Do( locateClub);
		
		CompanyClub company = null;
		
		if ( result.getStatus( ) == ResultStatus.OK)
		{
			company = result.getCompany( );
		}
		
		double discount = (company == null)? 0:company.getDiscount( );
		
		if ( discount == 1)
		{
			IVSDT vsdt = new VSDT();
			boolean isConnected = simulator.startConnection(
										vsdt.encrypt(userLogin), vsdt.encrypt(userPwd) );
			showSubscriptionResult( null, isConnected);
		}
		else
		{
			final CreditCardDetailsDialog creditCardDetailsDialog = new CreditCardDetailsDialog( );

			creditCardDetailsDialog.registerEventHandlerCancelCommand( CANCEL_CMD, 
					new ActionListener( )
			{

				@Override
				public void actionPerformed( ActionEvent arg0)
				{
					if ( arg0.getActionCommand( ).equalsIgnoreCase( CANCEL_CMD))
						creditCardDetailsDialog.dispose( );
				}

			});
			creditCardDetailsDialog.registerEventHandlerOkCommand( OK_CMD, 
					new ActionListener( )
			{

				@Override
				public void actionPerformed( ActionEvent arg0)
				{
					if ( arg0.getActionCommand( ).equalsIgnoreCase( OK_CMD))
					{
						String ccProvider = creditCardDetailsDialog
										.getCompanyName( );
						String cardNumber = creditCardDetailsDialog
										.getCardNumber( );
						String ccExpirationMonth = creditCardDetailsDialog
										.getExpirationMonth( );
						String ccExpirationYear = creditCardDetailsDialog
										.getExpirationYear( );
						String ccZIP = creditCardDetailsDialog.getZip( );

						try
						{
							IVSDT vsdt = new VSDT();
							boolean isConnected = simulator.startConnection(
									vsdt.encrypt(userLogin),  vsdt.encrypt(userPwd),
									vsdt.encrypt(cardNumber), vsdt.encrypt(ccProvider),
									Integer.parseInt( ccExpirationMonth), 
									Integer.parseInt( ccExpirationYear), Integer.parseInt( ccZIP));
							
							showSubscriptionResult( creditCardDetailsDialog, isConnected);

						}
						catch ( NumberFormatException e )
						{
							JOptionPane.showMessageDialog( creditCardDetailsDialog, 
				  					   					   Constants.INPUT_FIELDS_ERROR, 
				  					   					   Constants.ERROR_CAPTION_DLG, 
				  					   					   JOptionPane.ERROR_MESSAGE);

						}
						finally
						{
							creditCardDetailsDialog.dispose( );
						}
					}
				}
			});
			creditCardDetailsDialog.setVisible( true);
		}
	}
	
	/**
	 * Method shows the modal window dialog with relevant information
	 * regarding connection process.
	 * 
	 * @param dialog the owner dialog
	 * @param initPwd the initial password to show
	 */
	public void showSubscriptionResult(
			final CreditCardDetailsDialog dialog,
			boolean isConnected)
	{
		if ( isConnected) 
			JOptionPane.showMessageDialog( dialog, 
										   Constants.SECCESSEFUL_CONNECTION_MSG, 
										   "Welcome", 
										   JOptionPane.INFORMATION_MESSAGE);
		else
			JOptionPane.showMessageDialog( dialog, 
										   Constants.CONNECTION_ERROR_MSG, 
										   Constants.ERROR_CAPTION_DLG, 
										   JOptionPane.ERROR_MESSAGE);

	}		
}
